// UserConfig.jsx
import React, {useState, useEffect} from 'react';
import {Form, Input, Button, message, Row, Col, Space, Select} from 'antd';
import { request } from "../utils/requset";
import {SaveOutlined} from "@ant-design/icons";

const {Option} = Select;
const {TextArea} = Input;

const UserConfig = () => {
    const [basic_config] = Form.useForm();
    const [pdf_config] = Form.useForm();
    const [request_config] = Form.useForm();
    const [modelist, setModelist] = useState([]);
    const [selectMode, setSelectMode] = useState();
    const is_save_ht = [
        {value:"True",label:"是"},
        {value:"False",label:"否"},
    ]

    useEffect(() => {
        try {
            request.get('/rpa/sysConfig/get').then(response => {
                const data = response.data.data;
                setModelist(data.basic_config.login_way_list.split(',').map((login_way) => login_way.trim()));
                setSelectMode(data.basic_config.login_way)
                basic_config.setFieldsValue({
                    ...data.basic_config
                });
                pdf_config.setFieldsValue({
                    ...data.pdf_config
                });
                request_config.setFieldsValue({
                    ...data.request_config
                });
            });
        } catch (error) {
            if (error.response) {
                message.error(`获取配置失败: ${error.response.data.message}`);
            } else {
                console.error('获取配置失败:', error);
                message.error('获取配置失败，请稍后再试。');
            };
        }
    }, []);
    function onSave() {
        const basic_config_data = basic_config.getFieldsValue();
        console.log(basic_config_data)
        const pdf_config_data = pdf_config.getFieldsValue();
        const request_config_data = request_config.getFieldsValue();
        const payload = {
            "basic_config":{
                ...basic_config_data
            },
            "pdf_config":{
                ...pdf_config_data
            },
            "request_config":{
                ...request_config_data
            },
        }
        console.log(payload)
        request.post('/rpa/sysConfig/update', payload);
        message.success('保存成功！');
    }

    function modeChange(e) {
        setSelectMode(e);
    }

    return (
        <Row justify="center">
            <Col xs={24} sm={20} md={24} lg={24}>
                <div className="config-page-layout float-center" >
                    <h1>
                        用户配置
                        <Space style={{ float: 'right' }}>
                            <Button onClick={onSave}>
                                <SaveOutlined />保存
                            </Button>
                        </Space>
                    </h1>
                        <Form
                            form={basic_config}
                            layout="vertical"
                        >
                            <Row gutter={16}>
                                <Col xs={24} sm={12} md={12}>
                                    {/* 单位编号 */}
                                    <Form.Item
                                        label="单位编号"
                                        name="dwbh"
                                        rules={[{required: true, message: '请输入单位编号!'}]}
                                    >
                                        <Input placeholder="请输入单位编号"/>
                                    </Form.Item>
                                </Col>
                            </Row>
                            <Row gutter={16}>
                                <Col xs={24} sm={12} md={12}>
                                    {/* 登录方式选择 */}
                                    <Form.Item
                                        label="登录方式"
                                        name="login_way"
                                        rules={[{required: true, message: '请选择登录方式!'}]}
                                    >
                                        <Select placeholder="请选择登录方式" onChange={modeChange}>
                                            {modelist.map((mode) => (
                                                <Option key={mode} value={mode}>
                                                    {mode}
                                                </Option>
                                            ))}
                                        </Select>
                                    </Form.Item>
                                </Col>
                                <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="GSCloud 网址"
                                        name="smw_net"
                                        rules={[{required: true, message: '请输入 GSCloud 网址!'}, {
                                            type: 'url',
                                            message: '请输入有效的网址!'
                                        }]}
                                        hidden={selectMode!=='账号密码登录'}
                                    >
                                        <Input placeholder="请输入 GSCloud 网址"/>
                                    </Form.Item>
                                    <Form.Item
                                        label="GSCloud 网址"
                                        name="nw_net"
                                        rules={[{required: true, message: '请输入 GSCloud 网址!'}, {
                                            type: 'url',
                                            message: '请输入有效的网址!'
                                        }]}
                                        hidden={selectMode==='账号密码登录'}
                                    >
                                        <Input placeholder="请输入 GSCloud 网址"/>
                                    </Form.Item>
                                </Col>
                            </Row>
                            <Row gutter={16} hidden={selectMode!=='账号密码登录'}>
                                <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="GSCloud 用户名"
                                        name="gscloud_username"
                                        rules={[{required: true, message: '请输入 GSCloud 用户名!'}]}
                                    >
                                        <Input placeholder="请输入 GSCloud 用户名"/>
                                    </Form.Item>
                                </Col>
                                <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="GSCloud 密码"
                                        name="gscloud_password"
                                        rules={[{required: true, message: '请输入 GSCloud 密码!'}]}
                                    >
                                        <Input.Password placeholder="请输入 GSCloud 密码"/>
                                    </Form.Item>
                                </Col>
                            </Row>
                        </Form>
                    <h1>PDF配置</h1>
                        <Form
                            form={pdf_config}
                            layout="vertical"
                            onFinish={null}
                        >
                            <Row gutter={16}>
                                <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="检测合同页关键词"
                                        name="ht_key"
                                        rules={[{required: true, message: '请输入关键词!'}]}
                                    >
                                        <Input placeholder="请输入关键词"/>
                                    </Form.Item>
                                </Col>
                                <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="检测记账凭证页关键词"
                                        name="pz_key"
                                        rules={[{required: true, message: '请输入关键词!'}]}
                                    >
                                        <Input placeholder="请输入关键词"/>
                                    </Form.Item>
                                </Col>
                            </Row>
                            <Row gutter={16}>
                            <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="寻找凭证日期的表达式"
                                        name="pzrq_method"
                                        rules={[{required: true, message: '请输入表达式!'}]}
                                    >
                                        <Input placeholder="请输入表达式"/>
                                    </Form.Item>
                                </Col>
                                <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="寻找凭证号的表达式"
                                        name="pzh_method"
                                        rules={[{required: true, message: '请输入表达式!'}]}
                                    >
                                        <Input placeholder="请输入表达式"/>
                                    </Form.Item>
                                </Col>
                            </Row>
                            <Row gutter={16}>
                                <Col xs={24} sm={12} md={12}>
                                    {/* 登录方式选择 */}
                                    <Form.Item
                                        label="是否保留合同页"
                                        name="keep_ht"
                                        rules={[{required: true, message: '请选择是否保留!'}]}
                                    >
                                        <Select options={is_save_ht} />
                                    </Form.Item>
                                </Col>
                            </Row>
                        </Form>
                    <h1>接口配置</h1>
                        <Form
                            form={request_config}
                            layout="vertical"
                            onFinish={null}
                        >
                            <Row gutter={16}>
                                <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="域名"
                                        name="url_header"
                                        rules={[{required: true, message: '请输入域名!'}]}
                                    >
                                        <Input placeholder="请输入域名"/>
                                    </Form.Item>
                                </Col>
                            </Row>
                            <Row gutter={16}>
                                <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="打印列表请求URL"
                                        name="queryprintfsbzdj"
                                        rules={[{required: true, message: '请输入url!'}]}
                                    >
                                        <Input placeholder="请输入url"/>
                                    </Form.Item>
                                </Col>
                                <Col xs={24} sm={12} md={12}>
                                    <Form.Item
                                        label="打印列表请求体"
                                        name="queryprintfsbzdj_data"
                                        rules={[{required: true, message: '请输入请求体!'}]}
                                    >
                                        <TextArea placeholder="请输入请求体"/>
                                    </Form.Item>
                                </Col>
                            </Row>
                        </Form>
                </div>
            </Col>
        </Row>
    );
};

export default UserConfig;
